package com.sctu.adapter;


import android.app.Activity;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.sctu.EMSApp.R;
import com.sctu.bean.FamousItem;
import com.sctu.http.UrlConfig;
import com.sctu.httpvollry.GetVolleyHttp;

import java.util.List;


/**
 * Created by zhengsenwen on 2015/12/4.
 */
public class FamousAdapter extends BaseAdapter {


    private List<FamousItem.Results> list;
    private Activity context;

    public FamousAdapter(Activity context, List<FamousItem.Results> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        if (list != null) {
            return list.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_scenic_sight, null);


        TextView numberText = (TextView) view.findViewById(R.id.famous_item_number_text);
        ImageView spotImage = (ImageView) view.findViewById(R.id.famous_item_img);
        TextView nameText = (TextView) view.findViewById(R.id.famous_item_content);


        //设置每个listitem的图片
        GetVolleyHttp.getInstance(context).setImageLoader(spotImage, R.drawable.error_image, R.drawable.error_image, UrlConfig.URL + list.get(position).getPath());


        //设置编号
        String itemcontent = list.get(position).getTitle() + list.get(position).getContent().substring(0, 30) + "...";

        //设置文本内容
        //nameText.setText(famous.get(position).getContent());
        nameText.setText(itemcontent);
        numberText.setText(position + 1 + "");
        SpannableStringBuilder builder = new SpannableStringBuilder(nameText.getText().toString());
        ForegroundColorSpan span = new ForegroundColorSpan(context.getResources().getColor(R.color.gold));
        builder.setSpan(span, 0, list.get(position).getTitle().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.setSpan(new AbsoluteSizeSpan(42), 0, list.get(position).getTitle().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        nameText.setText(builder);

        return view;
    }
}
